﻿@model CountryModel

@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)

<script>
    $(document).ready(function () {
        bindBootstrapTabSelectEvent('country-edit');
    });
</script>

<div class="content">
    <div class="form-horizontal">
        <div id="country-edit" class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                @Html.RenderBootstrapTabHeader("tab-info", @T("Admin.Configuration.Countries.Info"), true)
                @Html.RenderBootstrapTabHeader("tab-states", @T("Admin.Configuration.Countries.States"))
            </ul>
            <div class="tab-content">
                @Html.RenderBootstrapTabContent("tab-info", @TabInfo(), true)
                @Html.RenderBootstrapTabContent("tab-states", @TabStates())
            </div>
        </div>
    </div>
</div>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "country-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}


@*save selected tab name*@
<input type="hidden" id="selected-tab-name" name="selected-tab-name" value="@(Html.GetSelectedTabName())">
@helper TabInfo()
{
    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                @(Html.LocalizedEditor<CountryModel, CountryLocalizedModel>("country-info-localized",
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Locales[item].Name)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(model => model.Locales[item].Name)
                            @Html.ValidationMessageFor(model => model.Locales[item].Name)
                        </div>
                    </div>
                    <div>
                        @Html.HiddenFor(model => model.Locales[item].LanguageId)
                    </div>
                </div>
    ,
                @<div>
                    <div class="form-group">
                        <div class="col-md-3">
                            @Html.SDFLabelFor(model => model.Name)
                        </div>
                        <div class="col-md-9">
                            @Html.SDFEditorFor(model => model.Name)
                            @Html.ValidationMessageFor(model => model.Name)
                        </div>
                    </div>
                </div>
    ))
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.AllowsBilling)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.AllowsBilling)
                        @Html.ValidationMessageFor(model => model.AllowsBilling)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.AllowsShipping)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.AllowsShipping)
                        @Html.ValidationMessageFor(model => model.AllowsShipping)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.TwoLetterIsoCode)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.TwoLetterIsoCode)
                        @Html.ValidationMessageFor(model => model.TwoLetterIsoCode)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.ThreeLetterIsoCode)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.ThreeLetterIsoCode)
                        @Html.ValidationMessageFor(model => model.ThreeLetterIsoCode)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.NumericIsoCode)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.NumericIsoCode)
                        @Html.ValidationMessageFor(model => model.NumericIsoCode)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.SubjectToVat)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.SubjectToVat)
                        @Html.ValidationMessageFor(model => model.SubjectToVat)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.SelectedProjectIds)
                    </div>
                    <div class="col-md-9">
                        <div class="row">
                            <div class="col-md-4">
                                @Html.EditorFor(model => model.SelectedProjectIds, new {SelectList = Model.AvailableProjects})
                                <script>
                                    $(document).ready(function() {
                                        var projectsIdsInput = $('#@Html.FieldIdFor(model => model.SelectedProjectIds)').data("kendoMultiSelect");
                                        projectsIdsInput.setOptions({
                                            autoClose: false
                                        });

                                        @if (Model.AvailableProjects.Count == 0)
                                        {
                                            <text>
                                    projectsIdsInput.setOptions({
                                        enable: false,
                                        placeholder: 'No Projects available'
                                    });
                                    projectsIdsInput._placeholder();
                                    projectsIdsInput._enable();
                                    </text>
                                        }
                                    });
                                </script>
                            </div>
                            <div class="col-md-8">
                                @Html.Action("MultiprojectDisabledWarning", "Common")
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.Published)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.Published)
                        @Html.ValidationMessageFor(model => model.Published)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.SDFLabelFor(model => model.DisplayOrder)
                    </div>
                    <div class="col-md-9">
                        @Html.SDFEditorFor(model => model.DisplayOrder)
                        @Html.ValidationMessageFor(model => model.DisplayOrder)
                    </div>
                </div>
            </div>
        </div>
    </div>
}
@helper TabStates()
{
    <div class="panel-group">
        <div class="panel panel-default">
            @if (Model.Id > 0)
            {
                <div class="panel-body">
                    <div id="states-grid"></div>

                    <script>
                        $(document).ready(function () {
                            $("#states-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("States", "Country", new { countryId = Model.Id }))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        },
                                        destroy: {
                                            url: "@Html.Raw(Url.Action("StateDelete", "Country"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: addAntiForgeryToken
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors",
                                        model: {
                                            id: "Id"
                                        }
                                    },
                                    error: function (e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    numeric: false,
                                    previousNext: false,
                                    info: false
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                columns: [{
                                    field: "Name",
                                    title: "@T("Admin.Configuration.Countries.States.Fields.Name")",
                                    width: 300
                                }, {
                                    field: "Abbreviation",
                                    title: "@T("Admin.Configuration.Countries.States.Fields.Abbreviation")",
                                    width: 150
                                }, {
                                    field: "Published",
                                    title: "@T("Admin.Configuration.Countries.States.Fields.Published")",
                                    width: 100,
                                    headerAttributes: { style: "text-align:center" },
                                    attributes: { style: "text-align:center" },
                                    template: '# if(Published) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                }, {
                                    field: "DisplayOrder",
                                    title: "@T("Admin.Configuration.Countries.States.Fields.DisplayOrder")",
                                    width: 100
                                }, {
                                    field: "Id",
                                    title: "@T("Admin.Common.Edit")",
                                    width: 100,
                                    template: "<input type='submit' value='@T("Admin.Common.Edit")' onclick=\"javascript:OpenWindow('@Url.Content("~/Admin/Country/StateEditPopup/")#=Id#?btnId=btnRefresh&formId=country-form', 800, 450, true); return false;\" class='btn btn-default' />"
                                }, {
                                    command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                                    title: "@T("Admin.Common.Delete")",
                                    width: 100
                                }]
                            });
                        });
                    </script>
                </div>
                <div class="panel-footer">
                    <div width="100%">
                        <button type="submit" id="btnAddNewState"
                                onclick="javascript:OpenWindow('@(Url.Action("StateCreatePopup", "Country", new {countryId = Model.Id, btnId = "btnRefresh", formId = "country-form"}))', 800, 450, true); return false;"
                                class="btn btn-primary">
                            @T("Admin.Configuration.Countries.States.AddNew")
                        </button>
                        <input type="submit" id="btnRefresh" style="display: none" />
                        <script type="text/javascript">
                            $(document).ready(function () {
                                $('#btnRefresh').click(function () {
                                    //refresh grid
                                    var grid = $("#states-grid").data('kendoGrid');
                                    grid.dataSource.read();

                                    //return false to don't reload a page
                                    return false;
                                });
                            });
                        </script>
                    </div>
                </div>
            }
            else
            {
                <div class="panel-body">
                    @T("Admin.Configuration.Countries.States.SaveBeforeEdit")
                </div>
            }
        </div>
    </div>
}